# -*- coding: utf-8 -*-

# Define here the models for your spider middleware
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html

import time

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from scrapy.http import HtmlResponse
from logging import getLogger

class SeleniumMiddleware(object):
    def __init__(self, timeout=None, service_args=[]):
        self.logger = getLogger(__name__)
        self.timeout = timeout
        self.driver = None  # 预先定义 driver 属性，初始值为 None

        options = Options()

        # 无头模式
        options.add_argument('--headless')

        # 配合上面的无界面化.
        options.add_argument('--disable-gpu')

        # 不加载图片, 提升速度
        options.add_argument('--blink-settings=imagesEnabled=false')

        # # 使用沙盒模式运行
        options.add_argument("--no-sandbox")

        # 启动无痕模式
        options.add_argument('--incognito')

        # 用户代理设置：模拟不同的浏览器或设备访问。
        options.add_argument(
            '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3')

        # 指定 Edge 浏览器的安装路径
        edge_binary_path = r"C:\Program Files (x86)\Microsoft\Edge Dev\Application\msedge.exe"  # 根据实际情况修改
        options.binary_location = edge_binary_path

        # 指定 Edge WebDriver 的路径
        edge_driver_path = r"E:\User\burn\Desktop\MyProject\Python\HelloWorld\webDriver\edge\msedgedriver.exe"
        service = Service(edge_driver_path)

        try:
            # 创建 Edge 浏览器实例
            self.driver = webdriver.Edge(service=service, options=options)
        except Exception as e:
            print(f"Failed to initialize Edge driver: {e}")

    def __del__(self):
        self.driver.close()

    def process_request(self, request, spider):
        if self.driver is None:
            print("Edge driver is not initialized.")
            return None

        self.logger.debug('Chrome is Starting')
        try:
            page = request.meta.get('page', 1)

            self.driver.get(request.url)

            body = self.driver.page_source.encode('utf-8')

            print(f"Page loaded successfully: {body}")

            if page > 1:
                input = self.wait.until(
                    EC.presence_of_element_located((By.XPATH, '//*[@id="J_bottomPage"]/span[2]/input')))
                button = self.wait.until(
                    EC.element_to_be_clickable((By.XPATH, '//*[@id="J_bottomPage"]/span[2]/a')))
                input.clear()
                input.send_keys(page)
                button.click()
            return HtmlResponse(url=request.url, body=self.driver.page_source, request=request, encoding='utf-8',
                                status=200)
        except TimeoutException:
            return HtmlResponse(url=request.url, status=500, request=request)

    def process_response(self, request, response, spider):
        """
        三个参数:
        # request: 响应对象所对应的请求对象
        # response: 拦截到的响应对象
        # spider: 爬虫文件中对应的爬虫类的实例对象, 可以通过这个参数拿到中的一些属性或方法
        """
        spider.browser.get(url=request.url)
        # 等待加载,  可以用显示等待来优化.
        time.sleep(1)
        row_response = spider.browser.page_source
        # 参数url指当前浏览器访问的url(通过current_url方法获取), 在这里参数url也可以用request.url
        return HtmlResponse(url=spider.browser.current_url, body=row_response, encoding="utf8",
                            request=request)

    @classmethod
    def from_crawler(cls, crawler):
        return cls(timeout=crawler.settings.get('SELENIUM_TIMEOUT'),
                   service_args=crawler.settings.get('CHROME_SERVICE_ARGS'))